package com.yc.d0422;

import com.yc.util.DBHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class FileDownload {

    public static void main(String[] args) throws IOException {
        downHtml("http://www.07347.com/index.html");
        //captureJobs();
    }

    public static String fileDir = "d:/a/07347/";

    public static void downHtml(String address) throws IOException {
        URL url = new URL(address);
        String filename = url.getFile();
        if(filename == null || filename.trim().isEmpty() == true){
            filename = "index.html";
        }
        String filepath = fileDir + filename;
        download(address, filepath);

        /**
         * 解析页面资源 :
         * css=><link href="...">,
         * js =><script src="..">,
         * 图片=><img src='...'>
         */

        final Document doc = Jsoup.parse(new File(filepath), "utf-8");
//        doc.select() ==> 相当于 js 的 document.querySelectorAll();
//        doc.selectFirst() ==> 相当于 js 的 document.querySelector();
        final Elements css = doc.select("link[rel=stylesheet]");
        css.forEach(element->{
            // 获取属性值
            final String href = element.attr("href");
            // 查找最后一个 /
            final int i = address.lastIndexOf("/");
            String webpath = address.substring(0,i+1) + href;
            String diskpath = fileDir + href;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        //http://www.baidu.com/page/user/demo.html
        final Elements js = doc.select("script[src]");
        js.forEach(element->{
            // 获取属性值
            final String src = element.attr("src");
            final int i = address.lastIndexOf("/");
            String webpath = address.substring(0,i+1) + src;
            String diskpath = fileDir + src;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        final Elements img = doc.select("img[src]");
        img.forEach(element->{
            // 获取属性值
            final String src = element.attr("src");
            final int i = address.lastIndexOf("/");
            String webpath = address.substring(0,i+1) + src;
            String diskpath = fileDir + src;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public static void download(String address, String filepath) throws IOException {
        System.out.println("开始下载文件");
        System.out.println("address = " + address);
        System.out.println("filepath = " + filepath);

        File file = new File(filepath);
        // 获取文件所在文件夹
        final File parentFile = file.getParentFile();
        if(parentFile.exists() == false){
            // 创建文件夹
            parentFile.mkdirs();
        }

        URL url = new URL(address);
        final URLConnection conn = url.openConnection();
        try (final InputStream in = conn.getInputStream();
             FileOutputStream fos = new FileOutputStream(filepath);
        ) {
            byte[] bytes = new byte[1024];
            int count;
            while ((count = in.read(bytes)) > -1) {
                // 输出到文件
                fos.write(bytes, 0, count);
            }
        }
        System.out.println("下载文件结束");

    }

    public static void captureJobs() throws IOException {
        String address = "http://www.07347.com/jobs/jobs_list.html";
        final Document doc = Jsoup.parse(new URL(address), 5000);
        final Elements elements = doc.select(".J_jobsList");
        elements.forEach(element -> {
            final Element a = element.selectFirst("a");
            final String href = a.attr("href");
            String webpath = "http://www.07347.com" + href;
            try {
                captureJob(webpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }


    public static void captureJob(String address) throws IOException {
        //String address = "http://www.07347.com/jobs/409.html";
        final Document doc = Jsoup.parse(new URL(address), 5000);
        String id = address.replaceAll(".+/(\\d+)\\.html","$1");
        // text() ==> innerText
        String name = doc.selectFirst(".j-n-txt").text();
        final Elements elements = doc.select(".itemli");
        String type = elements.get(0).text();
        String nums = elements.get(1).text();
        String dept = elements.get(2).text();
        String edu = elements.get(3).text();
        String works = elements.get(4).text();
        String ages = elements.get(5).text();
        String loaction = doc.selectFirst(".add").text().replace("工作地点","");
        String sql = "insert into job values(?,?,?,?,?,?,?,?,?)";
        DBHelper.update(sql,id,name,type,nums,dept,edu,works,ages,loaction);
    }


}
